import { Logger } from "./Logger";
import MathUtils from "./MathUtils";

export class LocationHelper {


    private static getDistance(longitude1: number, latitude1: number, longitude2: number, latitude2: number) {
        let radLat1: number = MathUtils.degreesToRadians(latitude1);
        let radLat2: number = MathUtils.degreesToRadians(latitude2);
        let a: number = radLat1 - radLat2;
        let b: number = MathUtils.degreesToRadians(longitude1) - MathUtils.degreesToRadians(longitude2);
        let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
            Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * 6378.137;// EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000; //输出为公里
        Logger.log("getDistance=====", s)
        return Math.floor(s * 1000)
    }

    //"23.258702000001627,113.28866699999783"   "1,1"
    public static JingWei2Distance(location1: string, location2: string) {
        let location1Arr: Array<string> = location1.split(",")
        if (location1Arr.length != 2) {
            return 0;
        }
        let location2Arr: Array<string> = location2.split(",")
        if (location2Arr.length != 2) {
            return 0;
        }
        let longitude1: number = parseFloat(location1Arr[0]); //经度
        let latitude1: number = parseFloat(location1Arr[1]); //纬度

        let longitude2: number = parseFloat(location2Arr[0]); //经度
        let latitude2: number = parseFloat(location2Arr[1]); //纬度

        let distance: number = this.getDistance(longitude1, latitude1, longitude2, latitude2)
        Logger.log("JingWei2Distance===", distance)
        return distance
    }
}